package com.share;

import hipro.comments.Comments;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;

public class CommentsActivity extends BaseActivity {

	private String username;
	private String password;
	private hipro.comments.Comments webserviceComments;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.event_list);

		// set view events
		webserviceComments = new Comments();
		webserviceComments
				.setWebservice(getString(R.string.webservice_comment));

		this.setValueAccount();

		// this.addComment();
		this.getComment();
		// this.removeComment();
		// this.getCommentsByEventId();
		 //this.getCommentsByUsername();
		//this.updateComment();
	}

	public void updateComment() {
		try {
			hipro.entities.Comments comment = new hipro.entities.Comments();
			comment = this.webserviceComments.getComment(this.username,
					this.password, 1);
			comment.setContent("I change content");
			boolean res = this.webserviceComments.updateComment(this.username,
					this.password, comment);

			AlertDialog al = new AlertDialog.Builder(this).create();
			al.setMessage("Update sucess " + res);
			al.show();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			
		}
	}

	public void getCommentsByUsername() {
		ArrayList<hipro.entities.Comments> list = new ArrayList<hipro.entities.Comments>();

		list = this.webserviceComments.getCommentsByUsername(this.username,
				this.password);
		String label = "";
		for (hipro.entities.Comments comment : list) {
			label += comment.getContent() + "  -   ";
		}

		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(label);
		al.show();
	}

	public void getCommentsByEventId() {
		ArrayList<hipro.entities.Comments> list = new ArrayList<hipro.entities.Comments>();

		list = this.webserviceComments.getCommentsByEventId(this.username,
				this.password, 1);
		String label = "";
		for (hipro.entities.Comments comment : list) {
			label += comment.getContent() + "  -   ";
		}

		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(label);
		al.show();
	}

	public void removeComment() {
		boolean res = this.webserviceComments.removeComment(this.username,
				this.password, 5);

		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(res + " remove");
		al.show();
	}

	public void getComment() {
		hipro.entities.Comments comment = new hipro.entities.Comments();
		comment = this.webserviceComments.getComment(this.username,
				this.password, 1);

		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(comment.getDate() + " is comment id");
		al.show();
	}

	public void addComment() {
		hipro.entities.Comments comment = new hipro.entities.Comments();
		comment.setUserId(1);
		comment.setEventId(1);
		comment.setContent("I just comment for this ticket");

		int idComment = this.webserviceComments.addComment(this.username,
				this.password, comment);
		AlertDialog al = new AlertDialog.Builder(this).create();
		al.setMessage(idComment + " is comment id");
		al.show();
	}

	/**
	 * Set value account login
	 */
	private void setValueAccount() {
		SharedPreferences settings = getSharedPreferences("pref_login", 0);
		this.username = settings.getString("username", "");
		this.password = settings.getString("password", "");

	}
}
